package com.kinon.markett2.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.kinon.markett2.R;
import com.kinon.markett2.tools.HttpTools;
import com.kinon.markett2.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class GongzuorenyuanActivity extends Activity {

    String token;
    HttpTools httptools = null;
    private TextView tv_fanhui;
    private TextView tv_username;
    private TextView tv_name;
    private TextView tv_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gongzuorenyuan);
        initRes();
    }

    private void initRes() {
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        token = sp.getString("data", "");
        tv_username = (TextView) findViewById(R.id.tv_username);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_fanhui = (TextView) findViewById(R.id.tv_fanhui);
        tv_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Info();
    }

    /**
     * 获取工作人员信息
     */
    private void Info() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        if (httptools == null) {
            httptools = new HttpTools(GongzuorenyuanActivity.this);
        }
        httptools.info(token, new JsonInfo());
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
    }

    public class JsonInfo extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
//			Log.i("info", "info---"+response.toString());
            try {
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    String username = object.getString("username");
                    String name = object.getString("name");
                    String cellphone = object.getString("cellphone");
//					et_pay.setText("");
//					handler.removeCallbacks(runnable);
//					Toast.makeText(JiesuanjineActivity.this, msg, Toast.LENGTH_SHORT).show();
//					finish();
                    tv_username.setText(username);
                    tv_name.setText(name);
                    tv_phone.setText(cellphone);
                } else {
//					handler.postDelayed(runnable, 1000);
//					et_pay.setText("");
                    Toast.makeText(GongzuorenyuanActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                              String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            Toast.makeText(GongzuorenyuanActivity.this, "网络请求超时",
                    Toast.LENGTH_SHORT).show();
        }
    }
}
